/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* IBust - IBus with Traditional interface
 * Copyright(c) 2013 Daniel Sun
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "ibusttext.h"

/* functions prototype */
static void         ibust_text_destroy      (IBustText            *text);
static gboolean     ibust_text_serialize    (IBustText            *text,
                                            GVariantBuilder     *builder);
static int          ibust_text_deserialize  (IBustText            *text,
                                            GVariant            *variant);
static gboolean     ibust_text_copy         (IBustText            *dest,
                                            const IBustText      *src);

G_DEFINE_TYPE (IBustText, ibust_text, IBUST_TYPE_SERIALIZABLE)

static void
ibust_text_class_init (IBustTextClass *class)
{
    IBustObjectClass *object_class = IBUST_OBJECT_CLASS (class);
    IBustSerializableClass *serializable_class = IBUST_SERIALIZABLE_CLASS (class);

    ibust_text_parent_class = (IBustSerializableClass *) g_type_class_peek_parent (class);

    object_class->destroy = (IBustObjectDestroyFunc) ibust_text_destroy;

    serializable_class->serialize   = (IBustSerializableSerializeFunc) ibust_text_serialize;
    serializable_class->deserialize = (IBustSerializableDeserializeFunc) ibust_text_deserialize;
    serializable_class->copy        = (IBustSerializableCopyFunc) ibust_text_copy;
}

static void
ibust_text_init (IBustText *text)
{
    text->is_static = TRUE;
    text->text = "";
    text->attrs = NULL;
}

static void
ibust_text_destroy (IBustText *text)
{
    if (text->text != NULL && text->is_static == FALSE) {
        g_free (text->text);
        text->text = NULL;
    }

    if (text->attrs) {
        g_object_unref (text->attrs);
        text->attrs = NULL;
    }

    IBUST_OBJECT_CLASS (ibust_text_parent_class)->destroy ((IBustObject *)text);
}

static gboolean
ibust_text_serialize (IBustText        *text,
                     GVariantBuilder *builder)
{
    gboolean retval;

    retval = IBUST_SERIALIZABLE_CLASS (ibust_text_parent_class)->serialize (
                        (IBustSerializable *)text, builder);
    g_return_val_if_fail (retval, FALSE);

    g_variant_builder_add (builder, "s", text->text);

    if (text->attrs == NULL) {
        text->attrs = ibust_attr_list_new ();
        g_object_ref_sink (text->attrs);
    }
    g_variant_builder_add (builder, "v", ibust_serializable_serialize ((IBustSerializable *)text->attrs));

    return TRUE;
}

static gint
ibust_text_deserialize (IBustText *text,
                       GVariant *variant)
{
    gint retval;
    retval = IBUST_SERIALIZABLE_CLASS (ibust_text_parent_class)->deserialize (
                            (IBustSerializable *)text, variant);

    if (text->is_static == FALSE)
        g_free (text->text);
    g_variant_get_child (variant, retval++, "s", &text->text);
    text->is_static = FALSE;

    if (text->attrs)
        g_object_unref (text->attrs);

    GVariant *var = g_variant_get_child_value (variant, retval++);
    text->attrs = IBUST_ATTR_LIST (ibust_serializable_deserialize (var));
    g_variant_unref (var);
    g_object_ref_sink (text->attrs);

    return retval;
}

static gboolean
ibust_text_copy (IBustText       *dest,
                const IBustText *src)
{
    gboolean retval;

    retval = IBUST_SERIALIZABLE_CLASS (ibust_text_parent_class)->copy (
                            (IBustSerializable *)dest,
                            (IBustSerializable *)src);
    g_return_val_if_fail (retval, FALSE);

    g_return_val_if_fail (IBUST_IS_TEXT (dest), FALSE);
    g_return_val_if_fail (IBUST_IS_TEXT (src), FALSE);

    dest->text = g_strdup (src->text);
    dest->is_static = FALSE;
    if (src->attrs) {
        dest->attrs = (IBustAttrList *)ibust_serializable_copy ((IBustSerializable *)src->attrs);
        g_object_ref_sink (dest->attrs);
    }

    return TRUE;
}

IBustText *
ibust_text_new_from_string (const gchar *str)
{
    g_assert (str);

    IBustText *text;

    text= g_object_new (IBUST_TYPE_TEXT, NULL);

    text->is_static = FALSE;
    text->text = g_strdup (str);

    return text;
}

IBustText *
ibust_text_new_from_ucs4 (const gunichar *str)
{
    g_assert (str);

    gchar *buf = g_ucs4_to_utf8 (str, -1, NULL, NULL, NULL);
    g_return_val_if_fail (buf != NULL, NULL);

    IBustText *text= g_object_new (IBUST_TYPE_TEXT, NULL);

    text->is_static = FALSE;
    text->text = buf;

    return text;
}

IBustText *
ibust_text_new_from_static_string (const gchar *str)
{
    g_assert (str);

    IBustText *text;

    text= g_object_new (IBUST_TYPE_TEXT, NULL);

    text->is_static = TRUE;
    text->text = (gchar *)str;

    return text;
}

IBustText *
ibust_text_new_from_printf (const gchar *format,
                           ...)
{
    g_assert (format);

    gchar *str;
    IBustText *text;
    va_list args;

    va_start (args, format);
    str = g_strdup_vprintf (format, args);
    va_end (args);

    g_return_val_if_fail (str != NULL, NULL);

    text= g_object_new (IBUST_TYPE_TEXT, NULL);
    text->is_static = FALSE;
    text->text = (gchar *)str;

    return text;
}

IBustText *
ibust_text_new_from_unichar (gunichar c)
{
    IBustText *text;
    gint len;

    g_return_val_if_fail (g_unichar_validate (c), NULL);

    text= g_object_new (IBUST_TYPE_TEXT, NULL);

    text->is_static = FALSE;
    text->text = (gchar *)g_malloc (12);
    len = g_unichar_to_utf8 (c, text->text);
    text->text[len] =  0;

    return text;
}

void
ibust_text_append_attribute (IBustText *text,
                            guint     type,
                            guint     value,
                            guint     start_index,
                            gint      end_index)
{
    g_assert (IBUST_IS_TEXT (text));

    IBustAttribute *attr;

    if (end_index < 0) {
        end_index  += g_utf8_strlen(text->text, -1) + 1;
    }

    if (end_index <= 0) {
        return;
    }

    if (text->attrs == NULL) {
        text->attrs = ibust_attr_list_new ();
    }

    attr = ibust_attribute_new (type, value, start_index, end_index);
    ibust_attr_list_append (text->attrs, attr);
}

guint
ibust_text_get_length (IBustText *text)
{
    return g_utf8_strlen (text->text, -1);
}

gboolean
ibust_text_get_is_static (IBustText *text)
{
    return text->is_static;
}

const gchar *
ibust_text_get_text (IBustText *text)
{
    return text->text;
}

IBustAttrList *
ibust_text_get_attributes (IBustText *text)
{
    return text->attrs;
}

void
ibust_text_set_attributes (IBustText     *text,
                          IBustAttrList *attrs)
{
    if (text->attrs)
        g_object_unref (text->attrs);
    text->attrs = attrs;
    g_object_ref_sink (text->attrs);
}
